import Vue from 'vue'
import Router from 'vue-router'
import {Message} from 'element-ui'
Vue.use(Router)
import { getCookie,clearAllCookies } from '@/utils/cookie'
/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * 如果设置，为true那就不在侧边栏进行展示
 * alwaysShow: true               if set true, will always show the root menu
 * 如果设置为true，那就总是显示根结点展示
 *                                if not set alwaysShow, when item has more than one children route,
 * 如果没有设置，如果有多个子路由，就会形成nest一组组件
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * 路由名称必须进行设置的字段
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    title是显示到侧边栏中的名称
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    图标可以使用element自带的图标进行使用，或者是项目里面进行自带使用
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    面包屑使用，如果为false，将不会显示到面包屑中
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/',
        redirect:'/login',
        hidden: true,//隐藏，不显示在侧边栏中
    },
    {
        path:'/login',
        name:'login',
        component: () => import('@/views/login/index'),
        hidden:true,
        beforeEnter:(to,from,next)=>{
            if(from.fullPath=='/'&&getCookie('token')){
                router.replace({
                    name:'dashboard',
                })
            }else{
                next()
            }
        }
    },
    {
        path: '/404',
        component: () => import('@/views/404/index'),
        hidden: true
    },

    {
        path: '/dashboard',
        component: Layout,
        // redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'dashboard',
                component: () => import('@/views/dashboard/index'),
                meta: { title: '数据统计', icon: 'dashboard'}
            }
        ]
    },
    {
        path:'/userManagement',
        component:Layout,
        children:[
            {
                path:'userManagement',
                name:'userManagement',
                component:()=>import('@/views/userManagement'),
                meta:{title:'用户管理',icon:'el-icon-user-solid'}
            }
        ]
    },
    {
        path:'/competition',
        component:Layout,
        meta:{title:'竞赛管理',icon:'el-icon-menu'},
        children:[
            {
                path:'competitonList',
                name:'competitonList',
                component:()=>import('@/views/competition/competitionList'),
                meta:{title:'竞赛列表',icon:'el-icon-s-grid'}
            },
            {
                path:'add',
                name:'add',
                component:()=>import('@/views/competition/competitionInfoMange/add'),
                meta:{title:'添加竞赛信息',icon:'el-icon-circle-plus'}
            },
            {
                path:'modify/:uid',
                name:'modify',
                component:()=>import('@/views/competition/competitionInfoMange/modify'),
                hidden: true,
                meta:{title:'修改竞赛信息',icon:'el-icon-remove'},
                props(route){
                    return {
                        ...route.params
                    }
                },
                beforeEnter:(to,from,next)=>{
                    if(from.fullPath=='/'){
                        router.replace({
                            name:'competitonList'
                        })
                    }else{
                        next()
                    }
                }
            },
            {
                path:'registration',
                name:'registration',
                component:()=>import('@/views/competition/registration'),
                meta:{title:'报名管理',icon:'el-icon-document'}
            },
        ]
    },
    {
        path:'/postManagement',
        component:Layout,
        children:[
            {
                name:'postManagement',
                path:'postManagement',
                component:()=>import('@/views/postManagement'),
                meta:{title:'召集管理',icon:'el-icon-price-tag'},
            }
        ]
    },
    // {
    //     path:'/statistics',
    //     component:Layout,
    //     children:[
    //         {
    //             name:'statistics',
    //             path:'statistics',
    //             component:()=>import('@/views/statistics'),
    //             meta:{title:'数据统计',icon:'el-icon-s-data'}
    //         }
    //     ]
        
    // },
    {

        path:'/permission',
        component:Layout,
        children:[
            {
                name:'permission',
                path:'permission',
                component:()=>import('@/views/permission'),
                meta:{title:'权限管理',icon:'el-icon-setting'}
            }
        ]
       
    },
    {
        path:'/log',
        component:Layout,
        children:[
            {
                name:'log',
                path:'log',
                component:()=>import('@/views/logManagement'),
                meta:{title:'日志管理',icon:'el-icon-s-release'}
            }
        ]
        
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
    // mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()


//前置路由守卫
router.beforeEach((to,from,next)=>{
    if(to.fullPath!='/login'){
        if(getCookie('token')){
            next()
        }else{
            Message({
                type:'error',
                showClose: true,
                duration:1000,
                message:'请登录后使用'
            })
            router.replace({
                name:'login'
            })
        }
    }else{
        next()
    }
        
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
